package com.springdemo.annother.parseexcel.util;

import com.springdemo.Util.HttpClientUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.util.StringUtils;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class PhoneUtil {


    public static String checkPhoneNumber(String phones) {
        if (phones.indexOf("/") != -1) {    //处理需要分割的电话号
            String[] phoneArray = phones.split("/");
            String phone = "";
            for (String s : phoneArray) {
                // 循环处理每个电话号码
                phone = phone + checkPhone(s) + "/";
            }
            phones = phone.substring(0, phone.length() - 1);
        } else {
            phones = checkPhone(phones);
        }
        return phones;
    }

    public static boolean checkIsPhone(String phone){
        String regex= "(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}";
       if (Pattern.matches(regex, phone)){
           return true;
       }
        return false;
    }


    public static String checkPhone(String phone) {

        if (!checkIsPhone(phone)) { // 座机号处理，电话号不处理
            //先判断是否是存在多个-超过一个，清楚重新处理
            if (phone.split("-").length>2){
                phone=phone.replace("-","");
            }
            if (phone.indexOf("-") == -1) {
                if (phone.length() == 12) { //处理不同位数的座机号 低于10位的暂不做处理
                    phone = phone.substring(0, 4) + "-" + phone.substring(4);
                } else if (phone.length() == 11||phone.length() == 10){
                    phone = phone.substring(0, 3) + "-" + phone.substring(3);
                }else {
                    phone =phone;
                }
            }
        }
        return phone;
    }



    private static Map<String,String> quhaoCach=new HashMap<>();

    public static String getQuhao(String city){
     // map.put("city",city);   Map<String,String> map = new HashMap<String, String>();
    //        //map.put("city",city);

   if (!StringUtils.isEmpty(quhaoCach.get(city))){
       return quhaoCach.get(city);
   }
    try {
            String url="https://quhao.51240.com/"+ URLEncoder.encode(city)+"__quhao/";
            //System.out.println(url);
            String s = HttpClientUtil.doGet(url,"utf-8");
            Document document = Jsoup.parse(s);
            Element table = document.getElementsByTag("table").last();
            Elements td = table.getElementsByTag("td");
            String quhao=td.get(1).childNode(0).toString();
//            String guishudi=(td.get(3).getAllElements().html());
//            map.put("区号",quhao);
            //map.put("归属地",guishudi);
        quhaoCach.put(city,quhao);
            return quhao;

        } catch (Exception e) {
            e.printStackTrace();
        }
//        map.put("quhao","异常");
        //map.put("guishudi","异常");
        return "";
    }



}
